﻿/*
 * Copyright 2008 Pimm Hogeling, Karim Sempel and Demian Sempel
 * 
 * This file is part of 8bitplays.
 * 
 * 8bitplays is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * 8bitplays is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with 8bitplays. If not, see <http://www.gnu.org/licenses/>.
 */
package nl.sandmen.team.gui {
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import nl.sandmen.team.data.Album;
	import nl.sandmen.team.events.AlbumEvent;
	import nl.sandmen.team.filters.Filters;
	/**
	* @author Team Sandmen
	*/
	public class AlbumSprite extends Sprite {
		private var _maskMode:Boolean;
		private var cover:Bitmap;
		private var maskRectangle:Sprite;
		public var title:String;
		public function AlbumSprite(album:Album, maskMode:Boolean = AlbumSpriteMaskModes.FRONT):void {
			_maskMode = maskMode;
			title = album.artist + " - " + album.name;
			filters = Filters.SIMPLE_OUTLINE;
			album.addEventListener(AlbumEvent.COVER_LOADED, coverLoaded);
			album.getCover();
		}
		private function _destroy():void {
			parent.removeChild(this);
			if (cover) {
				removeChild(cover);
				cover = null;
			}
			if (maskRectangle) {
				removeChild(maskRectangle);
				maskRectangle = null;
			}
		}
		private function coverLoaded(e:AlbumEvent):void {
			e.album.removeEventListener(AlbumEvent.COVER_LOADED, coverLoaded);
			maskRectangle = new Sprite();
			addChild(maskRectangle);
			cover = new Bitmap(e.cover.clone(), PixelSnapping.NEVER, true);
			addChild(cover);
			cover.x = -0x0E6
			cover.mask = maskRectangle;
			maskRectangle.graphics.beginFill(0);
			maskRectangle.graphics.drawRect(0, 0, 1, 246);
			maskRectangle.graphics.endFill();
			maskMode = _maskMode;
		}
		public function destroy():void {
			// Fade out, and then remove from the parent so it can be garbage collected.
			Tweener.addTween(this, {time:1, alpha:0, transition:"easeOutExpo", onComplete:_destroy});
		}
		public function get maskMode():Boolean {
			return _maskMode;
		}
		public function set maskMode(value:Boolean):void {
			if (value === AlbumSpriteMaskModes.FULL) {
				Tweener.addTween(maskRectangle, {time:.8, x:-0x0E6, width:0x1CD, transition:"easeOutCirc"});
			} else {
				Tweener.addTween(maskRectangle, {time:.8, x:0, width:0x0E6, transition:"easeOutCirc"});
			}
			_maskMode = value;
		}
		public function get scale():Number {
			return scaleX;
		}
		public function set scale(value:Number):void {
			scaleX = value;
			scaleY = value;
		}
	}
}